package com.mfin.bs.mappers.factory;

import com.mfin.bs.mappers.interfaces.IMapper;
import com.mfin.log.Logger;
import com.mfin.vo.interfaces.IMappable;

public class BaseMapperFactory extends Logger implements IMapperFactory {

	final String[][] MAPPERS = {
			{ "com.mfin.vo.finance.FinanceVO", "com.mfin.bom.interfaces.IFinance", 
				"com.mfin.bs.mappers.impl.FinanceMapper" },
			{ "com.mfin.bom.interfaces.IFinance", "com.mfin.vo.finance.FinanceVO",
					"com.mfin.bs.mappers.impl.FinanceMapper" },
					
			{ "com.mfin.bom.interfaces.ICustomer", "com.mfin.vo.CustomerVO",
					"com.mfin.bs.mappers.impl.CustomerMapper" },
			{ "com.mfin.vo.CustomerVO", "com.mfin.bom.interfaces.ICustomer",
					"com.mfin.bs.mappers.impl.CustomerMapper" },
					
			{ "com.mfin.bom.interfaces.IPartner", "com.mfin.vo.PartnerVO",
					"com.mfin.bs.mappers.impl.PartnerMapper" },
			{ "com.mfin.vo.PartnerVO", "com.mfin.bom.interfaces.IPartner",
					"com.mfin.bs.mappers.impl.PartnerMapper" },
					
			{ "com.mfin.bom.interfaces.IUser", "com.mfin.vo.UserVO",
					"com.mfin.bs.mappers.impl.UserMapper" },
			{ "com.mfin.vo.UserVO", "com.mfin.bom.interfaces.IUser",
					"com.mfin.bs.mappers.impl.UserMapper" },
					
			{ "com.mfin.vo.loan.LoanApplicationVO", "com.mfin.bom.interfaces.IFinanceLoan",
					"com.mfin.bs.mappers.impl.LoanMapper" },
			{ "com.mfin.bom.interfaces.IFinanceLoan", "com.mfin.vo.loan.FinanceLoanVO",
					"com.mfin.bs.mappers.impl.LoanMapper" },
					
			{ "com.mfin.vo.loan.LoanMasterVO", "com.mfin.bom.interfaces.ILoanMaster",
					"com.mfin.bs.mappers.impl.LoanMasterMapper" },
			{ "com.mfin.bom.interfaces.ILoanMaster", "com.mfin.vo.loan.LoanMasterVO",
					"com.mfin.bs.mappers.impl.LoanMasterMapper" },
					
			{ "com.mfin.vo.loan.LoanPreferenceVO", "com.mfin.bom.interfaces.ILoanPreference",
					"com.mfin.bs.mappers.impl.LoanPreferenceMapper" },
			{ "com.mfin.bom.interfaces.ILoanPreference", "com.mfin.vo.loan.LoanPreferenceVO",
					"com.mfin.bs.mappers.impl.LoanPreferenceMapper" },
					
			{ "com.mfin.vo.loan.DocChargeVO", "com.mfin.bom.interfaces.IDocCharge",
					"com.mfin.bs.mappers.impl.LoanPreferenceMapper" },
			{ "com.mfin.bom.interfaces.IDocCharge", "com.mfin.vo.loan.DocChargeVO",
					"com.mfin.bs.mappers.impl.LoanPreferenceMapper" },
					
			{ "com.mfin.vo.AddressVO", "com.mfin.bom.interfaces.IAddress",
					"com.mfin.bs.mappers.impl.AddressMapper" },
			{ "com.mfin.bom.interfaces.IAddress", "com.mfin.vo.AddressVO",
					"com.mfin.bs.mappers.impl.AddressMapper" } }; 

	private static BaseMapperFactory baseMapperFactory;

	private BaseMapperFactory() {
		super();
	}

	public static BaseMapperFactory getInstance() {
		if (baseMapperFactory == null) {
			baseMapperFactory = new BaseMapperFactory();
		}
		return baseMapperFactory;
	}

	@Override
	public IMapper getMapper(IMappable fromArg, IMappable toArg) {
		// TODO Auto-generated method stub

		try {
			for (String[] mapper : MAPPERS) {
				Class<?> fromClass = Class.forName(mapper[0]);
				Class<?> toClass = Class.forName(mapper[1]);

				if (fromClass.isAssignableFrom(fromArg.getClass())
						&& toClass.isAssignableFrom(toArg.getClass())) {
					return (IMapper) Class.forName(mapper[2]).newInstance();
				}
			}
		} catch (InstantiationException ine) {
			ine.printStackTrace();
		} catch (IllegalAccessException iae) {
			iae.printStackTrace();
		} catch (ClassNotFoundException cfe) {
			cfe.printStackTrace();
		}
		return null;
	}
}
